package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.Orders;
import com.example.buildtable.pojo.Recharge;
import com.example.common.util.R;
import com.example.buildtable.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "订单")
@RequestMapping(value = "/order/")
public class OrderController {
    @Autowired
    private OrderService orderService;
    
    @Operation(summary = "查询所有订单数据")
    @GetMapping(value = "/getAllOrder")
    public R getAllOrder() {
        LambdaJoinQueryWrapper<Orders> wrapper = new LambdaJoinQueryWrapper<>(Orders.class);
        wrapper.innerJoin(Recharge.class, Recharge::getRechargeId, Orders::getOrderRechargeId);
        List<Orders> orderList = orderService.list(wrapper);
        return R.ok("查询所有订单数据成功").data("orderList", orderList);
    }
    
    @Operation(summary = "增加订单")
    @PostMapping(value = "/addOrder")
    public R addOrder(Orders order) {
        boolean flag = orderService.save(order);
        if (flag) {
            return R.ok("订单增加成功");
        } else {
            return R.ok("订单增加失败");
        }
    }

    @Operation(summary = "删除订单")
    @GetMapping(value = "/deleteOrder/{orderId}")
    public R deleteOrder(@PathVariable("orderId") Integer orderId) {
        boolean flag = orderService.removeById(orderId);
        if (flag) {
            return R.ok("订单删除成功");
        } else {
            return R.ok("订单删除失败");
        }
    }

    @Operation(summary = "按照订单查看")
    @GetMapping(value = "/getOrderById/{orderId}")
    public R getOrderById(@PathVariable("orderId") Integer orderId) {
        LambdaJoinQueryWrapper<Orders> wrapper = new LambdaJoinQueryWrapper<>(Orders.class);
        wrapper.innerJoin(Recharge.class, Recharge::getRechargeId, Orders::getOrderRechargeId);
        wrapper.eq(Orders::getOrderId, orderId);
        Orders order = orderService.getOne(wrapper);
        if (order != null) {
            return R.ok("按照订单查看成功").data("order", order);
        } else {
            return R.error("按照订单查看失败");
        }
    }

    @Operation(summary = "修改订单")
    @PostMapping(value = "/updateOrder")
    public R updateOrder(Orders order) {
        boolean flag = orderService.updateById(order);
        if (flag) {
            return R.ok("订单修改成功");
        } else {
            return R.ok("订单修改失败");
        }
    }
}